package com.atguigu.gmall.product.service.impl;

import com.atguigu.gmall.product.mapper.SpuInfoMapper;
import com.atguigu.gmall.product.pojo.SpuImage;
import com.atguigu.gmall.product.pojo.SpuInfo;
import com.atguigu.gmall.product.pojo.SpuSaleAttr;
import com.atguigu.gmall.product.pojo.SpuSaleAttrValue;
import com.atguigu.gmall.product.service.SpuImageService;
import com.atguigu.gmall.product.service.SpuInfoService;
import com.atguigu.gmall.product.service.SpuSaleAttrService;
import com.atguigu.gmall.product.service.SpuSaleAttrValueService;
import com.atguigu.gmall.product.vo.SpuInfoVo;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

/**
* @author h8467
* @description 针对表【spu_info(商品表)】的数据库操作Service实现
* @createDate 2022-11-29 16:02:29
*/
@Service
public class SpuInfoServiceImpl extends ServiceImpl<SpuInfoMapper, SpuInfo>  implements SpuInfoService{
    @Autowired
    SpuImageService spuImageService;

    @Autowired
    SpuSaleAttrService spuSaleAttrService;

    @Autowired
    SpuSaleAttrValueService spuSaleAttrValueService;

    @Override
    public Page<SpuInfo> getPageBySpuId(Long pn, Long ps, Long c3id) {
        Page<SpuInfo> selectPage = baseMapper.selectPage(new Page<>(pn, ps), new LambdaQueryWrapper<SpuInfo>().eq(SpuInfo::getCategory3Id, c3id));
        return selectPage;
    }

    @Override
    public void saveSpuInfo(SpuInfoVo spuInfoVo) {
        List<SpuInfoVo.SpuImageListDTO> spuImageList = spuInfoVo.getSpuImageList();
        List<SpuInfoVo.SpuSaleAttrListDTO> spuSaleAttrListDTOList = spuInfoVo.getSpuSaleAttrList();


        // 1.上传SpuInfo表
        SpuInfo spuInfo = new SpuInfo();
        BeanUtils.copyProperties(spuInfoVo,spuInfo);
        baseMapper.insert(spuInfo);

        Long spuId = spuInfo.getId();

        // 2.上传spu照片信息
        List<SpuImage> spuImages = spuImageList.stream().map(spuImageListDTO -> {
            SpuImage spuImage = new SpuImage();
            BeanUtils.copyProperties(spuImageListDTO, spuImage);
            spuImage.setSpuId(spuId);
            return spuImage;
        }).collect(Collectors.toList());
        spuImageService.saveBatch(spuImages);

        // 3.上传spuSaleAttrList
        List<SpuSaleAttr> spuSaleAttrList = spuSaleAttrListDTOList.stream().map(spuSaleAttrListDTO -> {
            String saleAttrName = spuSaleAttrListDTO.getSaleAttrName();
            List<SpuInfoVo.SpuSaleAttrListDTO.SpuSaleAttrValueListDTO> spuSaleAttrValueListDTOList = spuSaleAttrListDTO.getSpuSaleAttrValueList();
            SpuSaleAttr spuSaleAttr = new SpuSaleAttr();

            // 3.1. SpuSaleAttrValue上传
            List<SpuSaleAttrValue> spuSaleAttrs = spuSaleAttrValueListDTOList.stream().map(spuSaleAttrValueListDTO -> {
                SpuSaleAttrValue spuSaleAttrValue = new SpuSaleAttrValue();

                BeanUtils.copyProperties(spuSaleAttrValueListDTO, spuSaleAttrValue);
                spuSaleAttrValue.setSaleAttrName(saleAttrName);
                spuSaleAttrValue.setSpuId(spuId);
                return spuSaleAttrValue;
            }).collect(Collectors.toList());
            spuSaleAttrValueService.saveBatch(spuSaleAttrs);


            BeanUtils.copyProperties(spuSaleAttrListDTO, spuSaleAttr);
            spuSaleAttr.setSpuId(spuId);
            return spuSaleAttr;
        }).collect(Collectors.toList());
        spuSaleAttrService.saveBatch(spuSaleAttrList);



    }
}




